package com.huang.annotation_reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//通过反射获取泛型
public class reflection09 {
    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }

    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Method method = reflection09.class.getMethod("test01", Map.class, List.class);
        //获得参数重泛型信息
        Type[] genericParameterTypes = method.getGenericParameterTypes();
        for (Type genericParameterType : genericParameterTypes) {
            System.out.println(genericParameterType);
            System.out.println("=============");
            //获得真实的泛型信息
            if(genericParameterType instanceof ParameterizedType){
                for (Type actualTypeArgument : ((ParameterizedType) genericParameterType).getActualTypeArguments()) {
                    System.out.println(actualTypeArgument);
                }

            }
        }
        //获得返回值泛型信息
        System.out.println("--------------------------" );
        Method method2 = reflection09.class.getMethod("test02");
        Type genericReturnType = method2.getGenericReturnType();
        if(genericReturnType instanceof ParameterizedType){
            for (Type actualTypeArgument : ((ParameterizedType) genericReturnType).getActualTypeArguments()) {
                System.out.println(actualTypeArgument);
            }

        }
    }
}
